package com.yanqu.road.server.cmd.cookboy;

import com.yanqu.road.entity.activity.cookboy.CookBoyMatch;
import com.yanqu.road.entity.activity.cookboy.CookBoyMatchDish;
import com.yanqu.road.entity.activity.cookboy.CookBoyMatchRank;
import com.yanqu.road.entity.enums.eCookBoyFoodStatusType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cookboy.CookBoyProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cookboy.CrossCookBoyMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.LinkedList;


@Cmd(code = Protocol.C_CROSS_COOK_BOY_MATCH_RANK, desc = "请求比赛排名")
public class CrossMatchRankCmd extends CrossCommand {

    private long userId;
    private int activityId;
    private int no;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CookBoyProto.CookBoyMatchRankReqMsg reqMsg = CookBoyProto.CookBoyMatchRankReqMsg.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        activityId = reqMsg.getActivityId();
        no = reqMsg.getNo();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CookBoyProto.CookBoyMatchRankRespMsg.Builder respMsg = CookBoyProto.CookBoyMatchRankRespMsg.newBuilder();
        CookBoyMatch cookBoyMatch = CrossCookBoyMgr.getCookBoyMatch(activityId,no);
        if(cookBoyMatch == null){
            ret = GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME;
        }
        respMsg.setRet(ret);
        if(ret == 0){
            LinkedList<CookBoyMatchRank> matchRankList = cookBoyMatch.getMatchRankList();
            int rank = 1;
            for(CookBoyMatchRank cookBoyMatchRank : matchRankList){
                CookBoyProto.CookBoyMatchRankTempMsg.Builder rankTemp = CookBoyProto.CookBoyMatchRankTempMsg.newBuilder();
                rankTemp.setRank(rank);
                rankTemp.setUserId(cookBoyMatchRank.getUserId());
                rankTemp.setTopScore(cookBoyMatchRank.getMatchDishList().get(0).getFraction());
                UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(cookBoyMatchRank.getUserId());
                if(userBaseInfo == null){
                    return;
                }
                rankTemp.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                for(CookBoyMatchDish cookBoyMatchDish : cookBoyMatchRank.getMatchDishList()){
                    CookBoyProto.CookBoyFinishFoodTempMsg.Builder dishTemp = CookBoyProto.CookBoyFinishFoodTempMsg.newBuilder();
                    dishTemp.setId(cookBoyMatchDish.getId());
                    dishTemp.setFoodId(cookBoyMatchDish.getFoodId());
                    dishTemp.setFraction(cookBoyMatchDish.getFraction());
                    dishTemp.setStatus(cookBoyMatch.getOpenTime() == 0 ? eCookBoyFoodStatusType.OnTable.getValue():eCookBoyFoodStatusType.IsOver.getValue());
                    dishTemp.setNo(cookBoyMatch.getNo());
                    dishTemp.setRank(cookBoyMatch.getDishRank(userId,cookBoyMatchDish.getId()));
                    dishTemp.setTime(0);//完成这道菜时间，没用
                    rankTemp.addFoodList(dishTemp);
                }
                respMsg.addRankList(rankTemp);
                rank++;
            }

        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_COOK_BOY_MATCH_RANK, respMsg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }

    @Override
    public void sendData() {

    }

}
